# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=geographiclib
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
pkgver=2.0
pkgrel=1
pkgdesc='Python implementation of the geodesic routines in GeographicLib (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://geographiclib.sourceforge.io/Python/doc/'
msys2_repository_url='https://github.com/geographiclib/geographiclib-python'
msys2_references=(
  'purl: pkg:pypi/geographiclib'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest")
options=('!strip')
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "${msys2_repository_url}/archive/v${pkgver}/${_realname}-python-${pkgver}.tar.gz")
sha256sums=('f7f41c85dc3e1c2d3d935ec86660dc3b2c848c83e17f9a9e51ba9d5146a15859'
            '3756afaaa5ca79e153827a2da972180bf045bd89c8415d643d9645f52f2cadef')

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"
  cp -r "${_realname}-python-${pkgver}/doc" "python-build-${MSYSTEM}/doc"
  cd "python-build-${MSYSTEM}"

  msg "Python build for ${MSYSTEM}"
  python -m build --wheel --skip-dependency-check --no-isolation

  msg "Build documentation"
  cd "${srcdir}/python-build-${MSYSTEM}/doc"
  sphinx-build . _build/html
}

check() {
  cd "python-build-${MSYSTEM}"

  python -m pytest
}

package_python-geographiclib() {
  cd "python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
}

package_python-geographiclib-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}"
  cp -a "${srcdir}/python-build-${MSYSTEM}/doc/_build/html" "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/html"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
